package com.wqtz.chechai.app;

import android.app.Application;
import android.content.Context;

import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVUser;
import com.bugtags.library.Bugtags;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.annotation.CacheLevel;
import com.okhttplib.annotation.CacheType;
import com.okhttplib.cookie.PersistentCookieJar;
import com.okhttplib.cookie.cache.SetCookieCache;
import com.okhttplib.cookie.persistence.SharedPrefsCookiePersistor;
import com.umeng.analytics.MobclickAgent;
import com.wqtz.chechai.utils.FileUtils;
import com.wqtz.chechai.utils.SDCardUtils;

import java.io.IOException;

import im.fir.sdk.FIR;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 11:07
 */

public class WQTZApplication extends Application {
	@Override
	public void onCreate() {
		super.onCreate();
		//BugTags SDK
		Bugtags.start("e875e7d43c911764ed4b848e4575c191", this, Bugtags.BTGInvocationEventBubble);
		//关闭umeng错误统计功能
		MobclickAgent.setCatchUncaughtExceptions(false);
		//禁止umeng默认的页面统计方式
		MobclickAgent.openActivityDurationTrack(false);
		//第三方更新SDK
		FIR.init(this);
		AVOSCloud.initialize(this, "6Yu15lD92nl3P7ke9mwp3ohm-gzGzoHsz", "gctqgO8QTEDxAz01SFYO8qdS");
		AVOSCloud.setDebugLogEnabled(true);
		AVAnalytics.enableCrashReport(this, true);
		AVUser currentUser = AVUser.getCurrentUser();
		if (currentUser != null) {
			// 跳转到首页
		} else {
			//缓存用户对象为空时，可打开用户注册界面…
		}
		OkHttpUtil.init(this)
				.setConnectTimeout(30)//连接超时时间
				.setWriteTimeout(30)//写超时时间
				.setReadTimeout(30)//读超时时间
				.setMaxCacheSize(10 * 1024 * 1024)//缓存空间大小
				.setCacheLevel(CacheLevel.FIRST_LEVEL)//缓存等级
				.setCacheType(CacheType.NETWORK_THEN_CACHE)//缓存类型
				.setShowHttpLog(true)//显示请求日志
				.setShowLifecycleLog(true)//显示Activity销毁日志
				.setRetryOnConnectionFailure(false)//失败后不自动重连
				.setDownloadFileDir(SDCardUtils.getSDCardRootDir()+"/wqtz")//文件下载保存目录
				/*.addResultInterceptor(HttpInterceptor.ResultInterceptor)//请求结果拦截器
				.addExceptionInterceptor(HttpInterceptor.ExceptionInterceptor)//请求链路异常拦截器*/
				.setCookieJar(new PersistentCookieJar(new SetCookieCache(),
						new SharedPrefsCookiePersistor(this)))//持久化cookie
				.build();

		try {
			//城市数据库初始化
			FileUtils.copyDBToDatabases(this);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static Context getApplication(){
		return getApplication();
	}

}
